package de.barny.obj;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;

import de.barny.prefs.ResourceBundleHelper;

public class Apple {
	
	Color appleColor = new Color(220, 180, 170);
	
	public Apple() {
		
	}
	
	int posX;
	int posY;

	public void paint(Graphics gameFieldGraphics) {
		Graphics2D apple = (Graphics2D)gameFieldGraphics;
		apple.setColor(appleColor);
		apple.fill(getShape());
	}
	
	private Shape getShape() {
		int dotSize = Integer.parseInt(getResource("snake.dot.size"));
		int roundness = Integer.parseInt(getResource("apple.roundness"));
		RoundRectangle2D.Double shape = null;
		shape = new RoundRectangle2D.Double((posX-1)*dotSize, posY*dotSize, dotSize, dotSize, roundness, roundness);
		return shape;
	}
	
	private String getResource(String a_strResourceKey) {
		return ResourceBundleHelper.getResource(a_strResourceKey);
	}
	
	public void setRandomPos() {
		int windowWidth = Integer.parseInt(getResource("window.size.width"));
		int windowHeight = Integer.parseInt(getResource("window.size.height"));
		int dotSize = Integer.parseInt(getResource("snake.dot.size"));
		
		int possiblePosX = windowWidth/dotSize;
		int possiblePosY = windowHeight/dotSize;
		
		posX = (int) (Math.random()*possiblePosX);
		posY = (int) (Math.random()*possiblePosY);
		
		if ((posX == 0 && posY <= 0)) {
			posX = 1;
			posY = 1;
		} else if (posX == 0) {
			posX = 1;
		} else if (posY == 0) {
			posY = 1;
		}
	}
	
	public int getPosX() {
		return posX;
	}
	
	public int getPosY() {
		return posY;
	}
}
